TeamPhotoshop
Reviews, updates and in depth guides to your favourite mobile games - AppGamer.com
Forum Home Latest Posts Search Help Subscribe

A little assistance

Page: 1 Reply
Jun 11th 2004#153081 Report
Member since: Mar 18th 2001
Posts: 1690
I created a script that will display and link to the contents of 3 different directories. However, when the directory contents are displayed, I also have the (.) current dir and (..) up one level displaying.

Anyone know how to filter those out?

Because I created this script at work, it is has become their intellectual property and I cannot post the code without their permission...soo...any help you can give without the code would be helpful.
Reply with Quote Reply
Jun 11th 2004#153085 Report
Member since: Nov 26th 2001
Posts: 2586
Just filter it out. Kind of depends on how you are getting the directories. But using a while loop or if statement:

[php]
while ( $link != "." || $link != ".." ) {

}
[/php]

or

[php]
if ( $link != "." || $link != ".." ) {

}
[/php]

You didn't specify which language, but this is pretty basic and should work on just about any language, some maybe with slight modification, but you get the point.
Reply with Quote Reply
Jun 11th 2004#153088 Report
Member since: Mar 18th 2001
Posts: 1690
Here is the script, excluding the rest of the HTML that formats the page. Please realize that I am no programmer and I have only basic knowledge of PHP (im trying to find time to learn more).

What this script does is reads the contents in 3 directories, then creates links to each file in the directory. At the same time, it displays the . and .. in the listing. I need to have those filtered out.

I am assuming that the while loop you are talking about would be pretty much the same as the while loop I have here, only with some additions...this is where I am lost.


[PHP]
//define directories
$bad_calls_dir = "badcalls/";
$good_calls_dir = "goodcalls/";
$impartial_calls_dir = "impartialcalls/";

//open directories & assign variables
$dir1 = opendir($bad_calls_dir);
$dir2 = opendir($good_calls_dir);
$dir3 = opendir($impartial_calls_dir);

//get directory entries
while($file_name = readdir($dir1))
{
$dir1_array[] = $file_name;
}
while($file_name = readdir($dir2))
{
$dir2_array[] = $file_name;
}
while($file_name = readdir($dir3))
{
$dir3_array[] = $file_name;
}

//close directories
closedir($dir1);
closedir($dir2);
closedir($dir3);

//count files in directories
$count1 = count($dir1_array);
$count2 = count($dir2_array);
$count3 = count($dir3_array);

//sort arrays
sort($dir1_array);
sort($dir2_array);
sort($dir3_array);


//print directory lists
//if directory is empty, nothing should print, if directory has files, should print the list of files below

//bad calls directory
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
for($count=0; $count< $count1; $count++)
{
print("\n");
print("\n");
print("\n");
}
print("

Bad Calls Listing:

Filename:

$dir1_array[$count]
\n");
print("


\n");

//good calls directory
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
for($count=0; $count< $count2; $count++)
{
print("\n");
print("\n");
print("\n");
}
print("

Good Calls Listing:

Filename:

$dir2_array[$count]
\n");
print("


\n");

//impartial calls directory
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
print("\n");
for($count=0; $count< $count3; $count++)
{
print("\n");
print("\n");
print("\n");
}
print("

Impartial Calls Listing:

Filename:

$dir3_array[$count]
\n");
print("


\n");

?>
[/PHP]
Reply with Quote Reply
Jun 11th 2004#153089 Report
Member since: Nov 26th 2001
Posts: 2586
This should work if I didn't fudge it up =)

[php]
//define directories
$bad_calls_dir = "badcalls/";
$good_calls_dir = "goodcalls/";
$impartial_calls_dir = "impartialcalls/";

//open directories & assign variables
$dir1 = opendir($bad_calls_dir);
$dir2 = opendir($good_calls_dir);
$dir3 = opendir($impartial_calls_dir);

//get directory entries
while($file_name = readdir($dir1))
{
$dir1_array[] = ( $file_name != "." || $file_name != ".." ) ? $file_name : continue;
}
while($file_name = readdir($dir2))
{
$dir2_array[] = ( $file_name != "." || $file_name != ".." ) ? $file_name : continue;
}
while($file_name = readdir($dir3))
{
$dir3_array[] = ( $file_name != "." || $file_name != ".." ) ? $file_name : continue;
}

//close directories
closedir($dir1);
closedir($dir2);
closedir($dir3);

//count files in directories
$count1 = count($dir1_array);
$count2 = count($dir2_array);
$count3 = count($dir3_array);
[/php]


Some notes:

Shy away from using short php opening tags, it's not going to be supported between all servers, esp if you don't have control over the php.ini file. I am not sure if you can override it in .htacces, but that is a lot of work when all you have to use is < ? php instead. Plus I get annoyed when I open an application that uses short tags and I have to convert 60+ files worth of tags.... my pet peave =)

Terniary statements is a one-liner substitute for if/else statements.

so you could also write:
[php]
if ( $file_name != "." || $file_name != ".." ) {
$dir1_array[] = $filename;
}else{
continue;
}
[/php]

You could also put in another while loop:
[php]
while ( $file_name != "." || $file_name != ".." ) {
$dir2_array[] = $file_name;
}
[/php]

But loops are one of the more cpu intensive processes. So if you have a large app with tons of loops, it can seem sluggish at times. But probably not an issue in you case so use it if you want.
Reply with Quote Reply
Jun 11th 2004#153090 Report
Member since: Mar 18th 2001
Posts: 1690
seems all of those options break the script. The first one hoses it up totally, doesnt even allow it to format the page using the HTML that is outside of the script. The second and third allow the formatting to work, I can see the tables, but there is not data in them...

Thanks for you help Marble...looks like im going to be trying to figure this one out later.
Reply with Quote Reply
Jun 11th 2004#153098 Report
Member since: Nov 26th 2001
Posts: 2586
Weird, it doesn't like negatives.... but it works the other way around:

[php]
while ($file_name = readdir($dir1)){
if ( $file_name == "." || $file_name == ".." ) {
continue;
}else{
$dir1_array[] = $file_name;
}
}
[/php]

I quickly tested it on my machine. Works from here.
Reply with Quote Reply
Jun 12th 2004#153113 Report
Member since: Mar 18th 2001
Posts: 1690
[QUOTE=Marble]Weird, it doesn't like negatives.... but it works the other way around:

[php]
while ($file_name = readdir($dir1)){
if ( $file_name == "." || $file_name == ".." ) {
continue;
}else{
$dir1_array[] = $file_name;
}
}
[/php]

I quickly tested it on my machine. Works from here.[/QUOTE]

I'll check it out once I get to work on monday. Thanks. Any suggestions on cleaning up the code a bit?
Reply with Quote Reply
Jun 14th 2004#153326 Report
Member since: Mar 18th 2001
Posts: 1690
[QUOTE=Axiom]I'll check it out once I get to work on monday. Thanks. Any suggestions on cleaning up the code a bit?[/QUOTE]


Awesome. It worked really well. Does exactly what I need it to do now. Also i actually learned something :p
Reply with Quote Reply
Page: 1 Back to top
Please login or register above to post in this forum